<?php
namespace app\home\controller;

use alipay\aop\AopClient;
use alipay\aop\request\AlipayTradePayRequest;
use alipay\aop\request\AlipayTradePrecreateRequest;
use alipay\aop\request\AlipayTradeQueryRequest;
use app\common\controller\Indexbase;

class Alipay extends Indexbase {
    //  打包支付宝配置
    public function conf(){
        $config = array (
            //应用ID,您的APPID。
            'app_id' => "{$this->webdb['alipayappid']}",
            //商户私钥
            'merchant_private_key' => "{$this->webdb['alipaykey']}",
            //异步通知地址
            'notify_url' => $this->webdb['www_url'].url('Play/alipayend'),
            //同步跳转
            'return_url' => $this->webdb['www_url'].url('Play/alipayend'),
            //编码格式
            'charset' => "UTF-8",
            //签名方式
            'sign_type'=>"RSA2",
            //支付宝网关
//            'gatewayUrl' => "https://openapi.alipaydev.com/gateway.do",
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
            //支付宝公钥
            'alipay_public_key' => "{$this->webdb['alipaygkey']}",
        );
        return $config;
    }
    //  支付宝扫码支付
    public function get_saoma($data){
        //  构造订单信息
        $datas["out_trade_no"] = trim($data["loid"]);
        $datas['subject'] = trim($data['title']);
        $datas['body'] = trim($data['title']);
        $datas['total_amount'] = round($data['pricemoney'] / 100,'2');
        //  引入支付宝SDK文件
        $config = $this->conf();
        vendor("/extend/alipay/AopSdk.php");
        $AopClient = EXTEND_PATH.'alipay/aop/AopClient.php';//引入
        $atpr = EXTEND_PATH.'alipay/aop/request/AlipayTradePrecreateRequest.php';//引入
        include($AopClient);
        include($atpr);
        //  配置支付宝信息
        $aop = new AopClient();
        $aop->gatewayUrl = $config['gatewayUrl'];
        $aop->appId = $config['app_id'];
        $aop->rsaPrivateKey = $config['merchant_private_key'];
        $aop->alipayrsaPublicKey=$config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        //  配置要发送的订单信息
        $request = new AlipayTradePrecreateRequest();
        $datas = json_encode($datas,LIBXML_NOCDATA);
        $request->setBizContent($datas);
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            $url = 'http://qr.liantu.com/api.php?text='.$result->$responseNode->qr_code;
            return $url;
        } else {
            return false;
        }
    }
    //  查询订单状态
    public function get_playend($data){
        $config = $this->conf();
        $AopClient = EXTEND_PATH.'alipay/aop/AopClient.php';//引入
        $atqr = EXTEND_PATH.'alipay/aop/request/AlipayTradeQueryRequest.php';//引入
        include($AopClient);
        include($atqr);
        $aop = new AopClient();
        $aop->gatewayUrl = $config['gatewayUrl'];
        $aop->appId = $config['app_id'];
        $aop->rsaPrivateKey = $config['merchant_private_key'];
        $aop->alipayrsaPublicKey=$config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $request = new AlipayTradeQueryRequest();
        $request->setBizContent(json_encode($data));
        $result = $aop->execute ($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        if($result->$responseNode->code == '10000' && $result->$responseNode->trade_status == 'TRADE_SUCCESS'){
            return $result;
        }
    }

}
